<?php
/* ###############################################################################
 *
 * $Id: bootstrap.php 66 2015-02-14 08:55:04Z expertsoftware.i@gmail.com $
 *
 * システム名
 *   PHP Web フレームワーク [Ponzu]
 *
 * 機能
 *   Bootstrap 3.x ライブラリ
 *
 * バージョン
 *   0.0.0_0
 *
 * ライセンス
 *   LGPL v2.0
 *
 * 履歴
 *   2014/07/01 K.Sonohara
 *     新規作成。
 *
 * ###############################################################################*/

function bootstrapHeader($full=false, $mainmenu=true, $mainmenuinverse=false, $mainmenudeep=1, $search=null, $description=false) {
  global $header_menus;
  global $main_menus;

  if (!$full) {
    printHTML('<div class="navbar navbar-inverse" role="navigation">');
  } else {
    printHTML('<div class="navbar" role="navigation">');
  }
  printHTML('<div class="container">');
  printHTML('<div class="navbar-header">');
  $logo = "";
  $title = SITE_TITLE;
  if (defined('SITE_LOGO')) {
    $logo = '<img src="' . SITE_LOGO . '" alt="' . SITE_TITLE . '" />';
    $title = "";
  }
  printHTML('<a class="navbar-brand" href="' . SITE_URL . '">' . $logo . $title . '</a>');
  printHTML('</div>');
  printHTML('<div class="collapse navbar-collapse">');
  if ($description) {
    printHTML('<ul class="nav navbar-nav navbar-left">');
    printHTML('<h4 class="nav-title">');
    printHTML(SITE_DESCRIPTION);
    printHTML('</h4>');
    printHTML('</ul>');
  }
  if ($header_menus != null) {
    printHTML('<ul class="nav navbar-nav navbar-right">');

    foreach ($header_menus->menus as $id=>$menu) {
      printHTML('<li>');
      $menu->printHTML();
      printHTML('</li>');
    }

    printHTML('</ul>');
  }
  if ($search != null) {
    printHTML('<form class="navbar-form navbar-right" role="search">');
    printHTML('<div class="form-group">');
    printHTML('<input type="text" class="form-control" placeholder="キーワード">');
    printHTML('</div>');
    printHTML('<button type="submit" class="btn btn-default">検索</button>');
    printHTML('</form>');
  }

  if ($mainmenuinverse && $mainmenu) {
    printHTML('</div>');
    printHTML('</div>');
    printHTML('</div>');
    printHTML('<div class="navbar navbar-inverse" role="navigation">');
    printHTML('<div class="container">');
    printHTML('<div class="navbar-collapse collapse">');
  }
  if ($mainmenu) {
    printHTML('<ul class="nav navbar-nav">');
    try {
      foreach ($main_menus->menus as $id => $menu) {
        if ($mainmenudeep > 0) {
          $m = false;

          if (count($menu->menus) > 0) {
            printHTML('<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $menu->title . '<span class="caret">&nbsp;</span></a>');
            foreach ($menu->menus as $cid => $cmenu) {
              if ($m) {
              } else {
                printHTML('<ul class="dropdown-menu">');
                $m = true;
              }
              printHTML('<li>');

              $l = $id . "/";
              if ($id == "-") $l = "";
              $cmenu->printHTML($l);
              printHTML('</li>');
            }
            if ($m) {
              printHTML('</ul>');
            }
            printHTML('</li>');
          } else {
            printHTML('<li>');
            $menu->printHTML("");
            printHTML('</li>');
          }
        } else {
          printHTML('<li>');
          $menu->printHTML("");
          printHTML('</li>');
        }
      }
    } catch (Exception $e) {
    }
    printHTML('</ul>');
  }

  printHTML('</div>');
  printHTML('</div>');
  printHTML('</div>');
}

function bootsrapToolbarButton($caption, $clazz, $icon=null, $disabled=false, $href="#", $onclick=null) {
  $h = "";
  $h .= '<button ';
  $h .= 'class="btn ';
  $h .= $clazz;
  if ($disabled) {
    $h .= ' disabled" ';
  } else {
    $h .= '" ';
  }
  if ($href) {
    $h .= ' href="';
    $h .= $href;
    $h .= '"';
  }
  if ($onclick) {
    $h .= ' onclick="';
    $h .= $onclick;
    $h .= '"';
  }
  $h .= '>';
  if ($icon) {
    $h .= '<span class="';
    $h .= $icon;
    $h .= '"> </span> ';
  }

  $h .= $caption;
  $h .= '</button>';

  printHTML($h);
};

function bootstrapFooter($sitemap=false) {
  global $main_menus;
  global $footer_menus;

  printHTML('<div class="navbar navbar-inverse" role="navigation" id="page_footer">');
  printHTML('<div class="container">');

  printHTML('<div class="navbar-collapse collapse">');
  if ($footer_menus != null) {
    printHTML('<ul class="nav navbar-nav navbar-right">');

    foreach ($footer_menus->menus as $id=>$menu) {
      printHTML('<li>');
      $menu->printHTML();
      printHTML('</li>');
    }

    printHTML('</ul>');
  }
  printHTML('</div>');


  printHTML('<div class="navbar-collapse collapse">');
  printHTML('<ul class="nav navbar-nav navbar-left">');
  $rss = null;
  if (defined('PAGE_RSS')) {
    $rss = PAGE_RSS;
  } else if (defined('SITE_RSS')) {
    $rss = SITE_RSS;
  }
  if ($rss != null) {
    printHTML('<li><a href="' . $rss . '" title="RSS"><img alt="RSS" src="' . SITE_URL . '/images/rss.png" />RSS</a></li>');
  }
  if (defined('PAGE_FACEBOOK_LIKE')) {
    $u = PAGE_FACEBOOK_LIKE;
    if ($u == null) {
      $u = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    }
    printHTML(facebookLikeButton($u));
  }
  printHTML('</ul>');

  $pd = null;
  if (defined('PAGE_DATE')) {
    $pd = PAGE_DATE;
  } else if (defined('SITE_DATE')) {
    $pd = SITE_DATE;
  }
  if ($pd != null) {
    printHTML('<ul class="nav navbar-nav navbar-right">');
    printHTML('<li><a>ページ更新日：' . $pd . '</a></li>');
    printHTML('</ul>');
  }

  printHTML('</div>');
  if ($sitemap) {
    printHTML('<hr />');
    printHTML('<div class="sitemap">');
    bootstrapSiteMap($main_menus);
    printHTML('</div>');
  }
  printHTML('<hr />');

  printHTML('<div class="navbar-collapse collapse">');
  printHTML('<ul class="nav navbar-nav navbar-left">');
  printHTML('<li class="navbar-text">' . SITE_COPYRIGHT . '</li>');
  printHTML('</ul>');
  printHTML('<ul class="nav navbar-nav navbar-right">');
  printHTML('<li class="navbar-text">' . SITE_FOOTER . '</li>');
  printHTML('</ul>');
  printHTML('</div>');
  printHTML('</div>');
  printHTML('</div>');
}

function bootstrapSiteMap($menus) {
  foreach ($menus->menus as $id=>$menu) {
    printHTML('<div class="col-xs-4">');
    printHTML('<ul>');
    printHTML('<li>');
    $menu->printHTML();
    printHTML('<ul>');
    foreach ($menu->menus as $i=>$m) {
      printHTML('<li>');
      $m->printHTML($id . "/");
      printHTML('</li>');
    }
    printHTML('</ul>');
    printHTML('</li>');
    printHTML('</ul>');
    printHTML('</div>');
  }
}

class CarouselItem {
  var $id; //id
  var $img = null;   //URL
  var $url = null;   //URL
  var $title = null; //ターゲット
  var $text = null; //ターゲット

  /**
   * コンストラクタ
   * @access public
   * @param id id
   * @param t タイトル
   * @param u URL
   * @param r ターゲット
   */
  function __construct($id, $img, $url=null, $title=null, $text=null) {
    $this->id = $id;
    $this->img = $img;
    $this->text = $text;
    $this->title = $title;
    $this->url = $url;
  }
}

function bootstrapLoadCarousel($name, $base_img=SITE_URL) {
  $file = explode("\n", loadTextFile($name));

  $id = $file[0];
  $img = $base_img . $file[1];
  $url = $file[2];
  $title = $file[3];
  $text = $file[4];

  return new CarouselItem($id, $img, $url, $title, $text);
}

/**
 * 
 * @access  public
 */
function bootstrapCarousels($count, $html="CAROUSEL_HTML", $button=false) {
  $dir = CAROUSEL_HOME;
  $files = listFiles($dir, array("txt"));

  $items = array();
  $c = count($files);
  if ($c > $count) $c = $count;

  shuffle($files);

  for ($i = 0; $i < $c; $i++) {
    $items[] = bootstrapLoadCarousel($files[$i]);
  }
  return bootstrapCarousel($items, $html, $button);
}

function CAROUSEL_HTML($item) {
  if ($item->title && $item->text) {
    printHTML('<div class="carousel-caption">');
    if ($item->title != null)
      printHTML('<h3>' . $item->title . '</h3>');
    if ($item->text != null)
      printHTML('<p>' . $item->text . '</p>');
    printHTML('</div>');
  }
}

function bootstrapCarousel($items, $html="CAROUSEL_HTML", $button=false) {
  if (count($items) > 0) {;
    printHTML('<div id="carousel-generic" class="carousel slide" data-ride="carousel" data-interval="10000">');

    printHTML('<ol class="carousel-indicators">');
    $count = 0;
    foreach ($items as $item) {
      $a = "";
      if ($count == 0) {
        $a = ' class="active"';
      }
      printHTML('<li data-target="#carousel-generic" data-slide-to="' . $count . '"' . $a . '> </li>');
      $count = $count + 1;
    }
    printHTML('</ol>');

    printHTML('<div class="carousel-inner">');
    $count = 0;
    foreach ($items as $item) {
      $a = "";
      if ($count == 0) {
        $a = ' active';
      }

      printHTML('<div class="item' . $a . '">');
      printHTML('<img class="carousel-image" src="' . trim($item->img) .  '" data-src="' . trim($item->img) .  '" alt="" />');
      call_user_func($html, $item);
      printHTML('</div>');
      $count = $count + 1;
    }
    printHTML('</div>');
    if ($button) {
      printHTML('<a class="left carousel-control" href="#carousel-generic" role="button" data-slide="prev">');
      printHTML('<span class="glyphicon glyphicon-chevron-left"></span>');
      printHTML('</a>');
      printHTML('<a class="right carousel-control" href="#carousel-generic" role="button" data-slide="next">');
      printHTML('<span class="glyphicon glyphicon-chevron-right"></span>');
      printHTML('</a>');
    }
    printHTML('</div>');
  }
}

function bootstrapSideMenu() {
  global $main_menus;

  printHTML('<div class="panel panel-info">');
  printHTML('<div class="panel-heading">');
  printHTML($main_menus->menus[PAGE_LEVEL]->title);
  printHTML('</div>');
  printHTML('<div class="list-group">');
  try {
    foreach ($main_menus->menus[PAGE_LEVEL]->menus as $menu) {
      if (PAGE_ID == $menu->id) {
        printHTML('<p class="list-group-item active">' . $menu->title . '</p>');
      } else {
        $l = PAGE_LEVEL . '/';
        if (PAGE_LEVEL == "-") $l = "";
        printHTML('<a class="list-group-item" href="' . SITE_URL . $l . $menu->id . '.php">' . $menu->title . '</a>');
      }
    }
  } catch (Exception $e) {
  }
  printHTML('</div>');
  printHTML('</div>');
}

function bootstrap_panel($type, $icon, $title, $body, $footer=null)
{
  print('<div class="panel panel-' . $type . '">');
  print('<div class="panel-heading">');
  print($icon . $header);
  print('</div>');
  if ($body != null) {
    print('<div class="panel-body"> ');
    print($body);
    print('</div>');
  }
  if ($footer != null) {
    print('<div class="panel-footer"> ');
    print($footer);
    print('</div>');
  }
  print('</div>');
}

function bootstrap_breadcrumb_item($menu) {
  print('<li>');
  $url = $menu->url;
  if ($menu->url) {
    print('<a href="' . SITE_URL . $url . '">');
    print $menu->title;
    print('</a>');
  } else {
    print $menu->title;
  }
}

function bootstrap_breadcrumb($tree, $menus) {
  print('<ol class="breadcrumb">');
  try {
    $menu = $menus;

    bootstrap_breadcrumb_item($menus);
    foreach ($tree as $key) {
      $menu = $menu->menus[$key];
      bootstrap_breadcrumb_item($menu);
    }

    print('<li class="active">');
    print PAGE_TITLE;
    print('</li>');
  } catch (Exception $e) {
  }
  print('</ol>');
}

function bootstrapForm($title, $formname, $url_json, $post_json, $confirm=true) {
  printHTML('<form class="form-horizontal" role="form" enctype="multipart/form-data" onsubmit=" return false; " name="' . $formname . '">');
  printHTML('<div class="row">');
  printHTML('<div class="col-xs-12">');
  printHTML('<h2>');
  printHTML($title);
  printHTML('</h2>');
  printHTML('<hr />');
  printHTML('</div>');
  printHTML('<div class="col-xs-offset-1 col-xs-11" id="form_message"> ');
  printHTML('</div>');
  printHTML('<div class="col-xs-offset-0 col-xs-12">');
  printHTML('<div id="form">読みこみ中・・・');

  $ss = ' mikan.form.html.load_json("form", "' . $url_json . '", "' . $formname . '"); ';

  printHTML('</div>');
  printHTML('<div class="row">');
  printHTML('<div class="col-xs-12">');
  printHTML('<hr />');
  printHTML('</div>');
  printHTML('<div class="col-xs-offset-6 col-xs-2">');
  $c = $confirm ? "true" : "false";
  printHTML('<button id="' . $formname . '_submit" class="disabled btn btn-info btn-lg btn-block" type="button" onclick="mikan.form.confirm_json(\'form\', \'' . $formname . '\', \'' . $post_json . '\', null, ' . $c . '); ">実行</button>');
  printHTML('</div>');
  printHTML('<div class="col-xs-2">');
  printHTML('<button id="' . $formname . '_back" class="hidden btn btn-lg btn-block" type="button" onclick=" mikan.form.html.back_json(\'form\', \'' . $formname . '\') ; ">戻る</button>');
  printHTML('</div>');
  printHTML('</div>');
  printHTML('</div>');
  printHTML('</div>');

  printHTML('</form>');

  return $ss;
}

function bootstrap_form_post_file($file_json, $dir) {
  include_once BASE_INC."module/3rd/json.php";

  $cjson = new Services_JSON();

  $result = array();
  $result['status'] = 1;
  try {
    $values = bootstrap_form_post($file_json);

    foreach ($values as $value) {
      if ($value->type == "file") {
        if (isset($_FILES[$value->name])) {
          print $_FILES[$value->name]["tmp_name"];
          if (is_uploaded_file($_FILES[$value->name]["tmp_name"])) {
            if (move_uploaded_file($_FILES[$value->name]["tmp_name"], $dir . $_FILES[$value->name]["name"])) {
            } else {
              $result['message'] = $_FILES[$value->name]["tmp_name"];
            }
          }
        }
      }
    }

    $result['status'] = 0;
  } catch(Exception $e) {
    $result['message'] = e;
  }

//  print $cjson->encode($result);
}

?>
